package com.ship.boats.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.dto.*;
import com.ship.boats.domain.vo.ExportCurrencyExchangeRateInfo;
import com.ship.boats.domain.vo.ExportOilinessPriceInfo;
import com.ship.boats.domain.vo.QueryCurrencyConversionInfo;
import com.ship.boats.domain.vo.QueryCurrencyExchangeRateInfo;
import com.ship.boats.service.CurrencyExchangeRateService;
import com.ship.boats.util.ExportDownloadUtil;
import com.ship.common.core.domain.R;
import com.ship.common.core.utils.poi.ExcelUtil;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>CurrencyExchangeRateController</b>
 *
 * @description: CurrencyExchangeRateController <br>
 * @date: 2024/3/19 17:02 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "币种汇率模块")
@RestController
public class CurrencyExchangeRateController {
    private static final Logger logger = LoggerFactory.getLogger(CurrencyExchangeRateController.class);

    @Autowired
    private CurrencyExchangeRateService currencyExchangeRateService;

    @Autowired
    private ExportDownloadUtil exportDownloadUtil;

    @ApiOperation("新增币种汇率")
    @PostMapping("/create/currency_exchange_rate")
    private JsonResult createCurrencyExchangeRate(@RequestBody CreateCurrencyExchangeRateRq rq){
        logger.info("新增币种汇率入参:{}"+ JSONUtil.parse(rq));
        boolean status = currencyExchangeRateService.createCurrencyExchangeRate(rq);
        return JsonResult.success(status);
    }

    @ApiOperation("根据币种汇率ID查询币种汇率详情")
    @GetMapping("/query/currency_exchange_rate/byId")
    public JsonResult queryCurrencyExchangeRateById(@RequestParam String id){
        QueryCurrencyExchangeRateInfo exchangeRateInfo = currencyExchangeRateService.queryCurrencyExchangeRateById(id);
        logger.info("根据币种汇率ID查询币种汇率详情返回结果:{}"+JSONUtil.parse(exchangeRateInfo));
        return JsonResult.success(exchangeRateInfo);
    }

    @ApiOperation("根据币种汇率ID修改币种汇率信息")
    @PostMapping("/update/currency_exchange_rate/byId")
    public JsonResult updateCurrencyExchangeRateById (@RequestBody UpdateCurrencyExchangeRateRq rq){
        logger.info("根据币种汇率ID修改币种汇率信息入参:{}"+JSONUtil.parse(rq));
        boolean status = currencyExchangeRateService.updateCurrencyExchangeRateById(rq);
        return JsonResult.success(status);
    }

    @ApiOperation("根据币种ID删除币种汇率信息")
    @GetMapping("/delete/currency_exchange_rate/byId")
    public JsonResult deleteCurrencyExchangeRateById(@RequestParam Long id){
        logger.info("删除"+id+"币种汇率信息");
        boolean status = currencyExchangeRateService.deleteCurrencyExchangeRateById(id);
        return JsonResult.success(status);
    }

    @ApiOperation("分页查询币种汇率信息列表")
    @PostMapping("/query/currency_exchange_rate/list")
    public JsonResult<PageInfo<QueryCurrencyExchangeRateInfo>> queryCurrencyExchangeRateList(@RequestBody QueryCurrencyExchangeRateRq rq){
        logger.info("分页查询币种汇率信息参数:{}"+JSONUtil.parse(rq));
        PageInfo<QueryCurrencyExchangeRateInfo> pageInfo = currencyExchangeRateService.queryCurrencyExchangeRateList(rq);
        logger.info("分页查询币种汇率信息列表返回结果:{}"+JSONUtil.parse(pageInfo));
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("币种汇率导出")
    @PostMapping("/export/currency_exchange_rate")
    public JsonResult exportCurrencyExchangeRateList(HttpServletResponse response,
                                               @RequestBody ExportCurrencyExchangeRateRq rq){
        logger.info("币种汇率导出参数:{}"+JSONUtil.parse(rq));
        List<ExportCurrencyExchangeRateInfo> exchangeRateInfo = currencyExchangeRateService.exportCurrencyExchangeRateList(rq);
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("curveDate");
        tableNameList.add("curveName");
        tableNameList.add("Value");
        Field[] fields = ExportCurrencyExchangeRateInfo.class.getDeclaredFields();
        String url = exportDownloadUtil.pageExport(exchangeRateInfo,fields,tableNameList, "币种汇率数据");
        return JsonResult.success(url);
    }

    @ApiOperation("币种换算")
    @PostMapping("/get/currencyConversion")
    public JsonResult queryCurrencyConversion(@RequestBody QueryCurrencyConversionRq rq){
        QueryCurrencyConversionInfo info = currencyExchangeRateService.queryCurrencyConversion(rq);
        return JsonResult.success(info);
    }
}
